<template>
	<view class="wrapper">
    <view class="search">
      <u-search
          placeholder="请输入关键字"
          v-model="keyword"
          height="60rpx"
          searchIconSize="50rpx"
          @search="onSearch"
          @custom="onSearch"
      ></u-search>
    </view>
    <swiper class="swiper" circular :indicator-dots="false" :autoplay="true" :interval="3000" :duration="500">
      <swiper-item
          v-for="(item, index) in home.swiper"
          :key="index"
          class="swpier-item"
      >
        <view class="image" @click="clickSwiper(item)">
          <u--image width="100%" height="100%" :fade="true" duration="450" lazy-load mode="widthFix" :src="baseUrl + item.image.path" v-if="item.image"></u--image>
        </view>
      </swiper-item>
    </swiper>
    <view class="news" v-if="home.news">
      <u-notice-bar
          :text="home.news.title"
          mode="link"
          fontSize="24rpx"
          color="#666"
          bgColor="#fff"
          @click="goNews"></u-notice-bar>
    </view>
    <view class="nav" v-if="home.nav && home.nav.data">
      <view
          v-for="(item, index) in home.nav.data"
          :key="index"
          class="nav-item"
          :class="{'more': home.nav.total == 5}"
          @click="clickNav(item)">
        <view class="image" v-if="item.image && item.image.path" >
          <u--image lazy-load width="100%" height="100%" :fade="true" duration="450" mode="widthFix" :src="baseUrl + item.image.path" v-if="item.image"></u--image>
        </view>
        <span class="name">{{ item.name }}</span>
      </view>
    </view>
    <view class="product" v-if="false">
      <view v-for="(item, index) in home.recommend" :key="index" class="product-item" @click="clickProduct(item)">
        <view class="image">
          <u--image lazy-load width="100%" height="100%" :fade="true" duration="450" mode="widthFix" :src="baseUrl + item.mainImage.path" v-if="item.mainImage"></u--image>
        </view>
        <view class="desc">
          <view class="title">{{ item.title }}</view>
          <view class="price_old">{{ item.title_sub }}</view>
          <view class="price">￥{{ item.price }}</view>
          <view class="button">立即购买</view>
        </view>
      </view>
    </view>
    <u-sticky :offset-top="top">
      <view class="category">
        <u-tabs
          :list="categoryList"
          :current="current"
          @change="handleCategory"
          :activeStyle="{
              color: 'red',
          }"
          lineColor="red"
          lineHeight="5"
        ></u-tabs>
      </view>
    </u-sticky>
    <view class="product">
      <v-list :list="productList" :show-nothing="false" />
      <view @click="moreProduct" class="more" v-if="current > 0 && productList.length > 0">点击查看更多</view>
    </view>
  </view>
</template>

<script>
import config from '../../config/config'
import base64 from '../../util/base64'
import Home from '../../api/home'
import List from '../../components/list'
const homeModel = new Home()

export default {
  components: {
    'v-list': List
  },
  data() {
    return {
      top: 0,
      home: {},
      baseUrl: config.baseUrl,
      productList: [],
      categoryList: [],
      keyword: '',
      // 推荐商品
      recommendProduct: [],
      current: 0
    }
  },
  onShow() {
    /* #ifdef H5 */
    this.top = 45
    /* #endif */
  },
  onLoad() {
    uni.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#00a49c',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.fetchData()
  },
  onPullDownRefresh() {
    this.current = 0
    this.keyword = ''
    this.fetchData()
  },
  methods: {
    fetchData() {
      homeModel.getHome().then(response => {
        let { data } = response
        data = base64.parse(data)
        this.home = data
        this.recommendProduct = this.home.recommend
        this.categoryList = [{ name: '推荐商品' }, ...this.home.category]
        // console.log(data)
        this.productList = [...this.recommendProduct]
        uni.stopPullDownRefresh();
      })
    },
    goNews() {
      uni.navigateTo({
        url: `/sub_service/news/detail?id=${this.home.news.id}`
      })
    },
    clickSwiper(item) {
      if (item.url) {
        uni.navigateTo({
          url: item.url
        })
      }
    },
    clickNav(item) {
      if (item.url) {
        uni.navigateTo({
          url: item.url
        })
      }
    },
    clickProduct(item) {
      uni.navigateTo({
        url: `/sub_product/detail/index?id=${item.id}`
      })
    },
    // 分类商品
    handleCategory(tab) {
      this.current = tab.index
      // console.log(tab)
      if (!tab.id) {
        this.productList = [...this.recommendProduct]
      } else {
        const id = this.categoryList[this.current].id
        homeModel.getCateProduct({ id }).then(response => {
          let { data } = response
          data = base64.parse(data)
          this.productList = [...data.list]
        })
      }
    },
    // 更多分类产品
    moreProduct() {
      const id = this.categoryList[this.current].id
      uni.navigateTo({
        url: `/sub_product/category/index?id=${id}`
      })
    },
    onSearch() {
      if (this.keyword) {
        uni.navigateTo({
          url: `/sub_common/search/index?keyword=${this.keyword}`
        })
      } else {
        uni.showToast({
          icon: 'none',
          title: '请输入关键字'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
$swiper_height: 335rpx;
.wrapper {
  padding: 0 20rpx;
  background: $background_color;
  padding-top: 10px;
  .swiper {
    height: $swiper_height;
    .image {
      //background: red;
      height: $swiper_height;
      border-radius: 10rpx;
      overflow: hidden;
      image {
        width: 100%;
        height: 100%;
        display: block;
        margin: auto;
      }
    }
  }
  .nav {
    margin: 20rpx 0;
    background: white;
    display: flex;
    flex-wrap: wrap;
    border-radius: 10rpx;
    justify-content: space-around;
    .nav-item {
      //width: 25%;
      width: 20%;
      &.more {
        width: 20%;
      }
      margin-bottom: 10rpx;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .image {
        padding: 30rpx;
        overflow: hidden;
        //background: red;
        image {
          width: 100%;
          height: 100%;
          display: block;
          margin: auto;
        }
      }
      .name {
        font-size: 24rpx;
        color: #000;
        //font-weight: 700;
        height: 60rpx;
        line-height: 60rpx;
        margin-top: -20rpx;
        text-align: center;
      }
    }
  }
  .news {
    margin: 20rpx 0;
    border-radius: 10rpx;
    overflow: hidden;
  }
  .search {
    background: white;
    margin: 0rpx 0;
    border-radius: 10rpx;
    overflow: hidden;
    box-sizing: border-box;
    padding: 10px;
    margin-bottom: 10px;
  }
  .category {
    background: white;
    margin: 20rpx 0;
    border-radius: 10rpx;
    overflow: hidden;
    box-sizing: border-box;
    padding: 0 10px;
  }
  .product {
    padding-bottom: 20px;
    .more {
      padding: 20rpx;
      border-radius: 10rpx;
      background: white;
      font-size: 24rpx;
      color: #666;
      text-align: center;
    }
    .product-item {
      display: flex;
      border-radius: 10rpx;
      padding: 20rpx;
      margin-bottom: 20rpx;
      background: white;
      .image {
        width: 240rpx;
        height: 240rpx;
        padding: 10rpx;
        overflow: hidden;
        image {
          width: 100%;
          height: 100%;
          display: block;
          margin: auto;
        }
      }
      .desc {
        flex: 1;
        text-align: left;
        position: relative;
        margin-top: 20rpx;
        padding-left: 20rpx;
        font-size: 24rpx;
        .title {
          font-weight: 700;
          font-size: 24rpx;
          line-height: 40rpx;
        }
        .price_old, .price {
          line-height: 36rpx;
          color: #808080;
        }
        .price {
          color: red;
          font-size: 30rpx;
          font-weight: 700;
          height: 80rpx;
          line-height: 80rpx;
        }
        .button {
          position: absolute;
          background: $main_color;
          color: white;
          padding: 15rpx 30rpx;
          right: 0;
          bottom: 0;
          font-weight: 500;
          border-radius: 30rpx;
        }
      }
    }
  }
}
</style>
